<template>
  <router-view v-slot="{ Component }">
    <transition name="slide">
      <component :is="Component"></component>
    </transition>
  </router-view>
  <!-- 新版本下载 -->
  <a :href="updatePath" download id="uploadApk" style="position:fixed;left:0px;bottom:-30px"
    :style="{ opacity: isDevelopment ? 1 : 0 }">apk下载地址</a>
</template>

<script lang="ts">
declare const process: any
declare const window: any

import { Dialog } from 'vant'
import { type BaseSystemInfo } from '@/server/android-vue'
import { APP_CONFIG_KEY } from './server/base'
export default {
  data() {
    return {
      isFirst: true,
    }
  },
  computed: {
    isDevelopment() {
      try {
        return (process as any).env.NODE_ENV !== 'production'
      } catch (error) {
        return false
      }
    },
    getAppLineInfo() {
      try {
        if (window._APK_UPDATE_CONFIG_INFO && window._APK_UPDATE_CONFIG_INFO[APP_CONFIG_KEY]) {
          return window._APK_UPDATE_CONFIG_INFO[APP_CONFIG_KEY];
        } else {
          throw new Error('没有找到相关配置！');
        }
      } catch (e) {
        return {
          text: "智慧工单管理系统",
          VERSION_CODE: 6,
          downloadPath: "http://vsuzeqbigdata/apks/hd.apk",
          downloadHistory: ["http://vsuzeqbigdata/apks/hd.apk"],
          voicePath: 'http://vsuzeqbigdata/mp3/xinwenlianbo.mp3',
          log: ''
        }
      }
    },
    updatePath() {
      try {
        return this.getAppLineInfo.downloadPath;
      } catch (error) {
        return '';
      }
    },
    isUpdate() {
      try {
        const that = this as any;
        if (!that.$native) {
          return false;
        }
        const systemInfo = that.$native.getSystemInfo() as BaseSystemInfo;
        if (!systemInfo || systemInfo.VERSION_CODE >= this.getAppLineInfo.VERSION_CODE) {
          return false;
        }
        return true;
      } catch (error) {
        return false;
      }
    },
  },
  methods: {/**
     /**
     * 下载更新
     */
    updateApk() {
      setTimeout(() => {
        if (!this.isUpdate) {
          return;
        }
        Dialog.confirm({
          title: '更新提示',
          message: '发现了新版本，是否更新软件',
          theme: 'round-button',
          confirmButtonText: this.getAppLineInfo.VERSION_CODE <= 2 ? '查看地址' : '去更新'
        }).then(() => {
          if (this.getAppLineInfo.VERSION_CODE <= 2) {
            Dialog.alert({
              title: '长按地址进行复制，再使用浏览器进行下载',
              message: this.updatePath,
              theme: 'round-button',
            });
            return;
          }
          try {
            /**
             * 下载文件
             */
            (this as any).$native.downloadFile(this.updatePath);
          } catch (error) {
          }
        }).catch(() => {
        }).finally(() => {

        });


      }, 1000)
    },
  },
  mounted() {
    //查看更新
    if (this.isFirst) {
      setTimeout(() => {
        this.updateApk();
      }, 1000);
      this.isFirst = false;
    }
  },
  created() {

  },

}
</script>

<style>
@import url('@/assets/font.css');
</style>
<style lang="scss">
#app {
  margin: 0px;
  padding: 0px;
  overflow: hidden;
  box-sizing: border-box;
  height: 100%;
  width: 100%;


  // .slide-leave-active,
  // .slide-enter-active {
  //   transition: all .5s ease;
  // }


  // .slide-enter-from,
  // .slide-leave-to {
  //   top: -100%;
  //   opacity: 0;
  // }


  .slide-enter-active {
    transition: all 0.6s ease;
  }

  .slide-enter-from {
    position: fixed;
    opacity: 0;
    // transform: translateY(100%);
  }

  .slide-leave-active {
    transition: all 0.3s ease;
  }

  .slide-leave-to {
    position: fixed;
    opacity: 0;
    // transform: translateY(-100%);
  }

}
</style>
